﻿Public Class SP_Loai
    Private spBL As SanPhamBL
    Private spDT As SanPhamDT

    Private Sub loadDGV(ByVal dieukien As String)
        spBL = New SanPhamBL
        Dim list As New List(Of SanPhamDT)
        list = spBL.GetData(dieukien)
        dgv.DataSource = list
        For i As Integer = 0 To dgv.Rows.Count - 1
            If Not spBL.isSelling(Convert.ToInt32(dgv.Rows(i).Cells(0).Value)) Then
                dgv.Rows(i).DefaultCellStyle.BackColor = Color.Aqua
            End If
        Next
        dgv.Columns(4).HeaderText = ""
        dgv.Columns(4).Width = -1

        dgv.ClearSelection()
    End Sub

    Private Sub CBox()
        Dim dt As DataTable
        Dim cn As New DatabaseConnect
        dt = New DataTable()
        dt = cn.GetData("select DisTinct * from SanPham_DonViTinh")
        cbDVT.DataSource = dt
        cbDVT.DisplayMember = "donvitinh"
        cbDVT.ValueMember = "donvitinhID"
        dt = New DataTable()
        dt = cn.GetData("select DisTinct * from SanPham_TriGia")
        cbTriGia.DataSource = dt
        cbTriGia.DisplayMember = "trigia"
        cbTriGia.ValueMember = "trigiaID"
    End Sub

    Private Sub SP_Loai_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadDGV(" WHERE MaLoai <> -1")
        CBox()
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        txtTen.Text = ""
        txtDVT.Text = ""
        txtTGKhac.Text = ""
        btnThem.Enabled = True
        btnCapNhat.Enabled = True
        gbTTSP.Enabled = False
        dgv.Enabled = True
    End Sub

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        If dgv.SelectedRows.Count > 0 Then
            btnThem.Enabled = False
            btnCapNhat.Enabled = False
            gbTTSP.Enabled = True
            txtTen.Text = dgv.SelectedRows(0).Cells(1).Value.ToString()
            cbTriGia.SelectedValue = dgv.SelectedRows(0).Cells(2).Value
            cbDVT.SelectedValue = dgv.SelectedRows(0).Cells(3).Value
            dgv.Enabled = False
        Else
            Return
        End If
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        gbTTSP.Enabled = True
        btnCapNhat.Enabled = False
        btnThem.Enabled = False
        dgv.Enabled = False
        dgv.ClearSelection()
    End Sub

    Private Sub them_trigia()
        Dim cn As New DatabaseConnect
        cn.Execute("insert into SanPham_TriGia(trigia) values(" & Double.Parse(txtTGKhac.Text) & ")")
    End Sub
    Private Sub them_dvt()
        Dim cn As New DatabaseConnect
        cn.Execute("insert into SanPham_DonViTinh(donvitinh) values(N'" + txtDVT.Text + "')")
    End Sub

    Private Sub xuli_suathem()
        Dim trigia As Integer = cbTriGia.SelectedValue
        Dim donvitinh As Integer = cbDVT.SelectedValue
        Dim cn As New DatabaseConnect
        Dim dt As DataTable
        If chboxTG.Checked = True Then
            them_trigia()
            dt = New DataTable()
            dt = cn.GetData("SELECT COUNT(trigiaID) as currentID FROM SanPham_TriGia")
            trigia = Convert.ToInt32(dt.Rows(0)("currentID"))
        End If
        If chboxDVT.Checked = True Then
            them_dvt()
            dt = New DataTable()
            dt = cn.GetData("SELECT COUNT(donvitinhID) as currentID FROM SanPham_DonViTinh")
            donvitinh = Convert.ToInt32(dt.Rows(0)("currentID"))
        End If

        If dgv.SelectedRows.Count > 0 Then 'cap nhat
            If txtTen.Text <> "" Then
                Dim ma As Integer = Convert.ToInt32(dgv.SelectedRows(0).Cells(0).Value)
                spDT = New SanPhamDT(ma, txtTen.Text, trigia, donvitinh)
                spBL = New SanPhamBL
                spBL.InUp(spDT, False)
                MessageBox.Show("Cập nhật thành công!")
            Else
                MessageBox.Show("Chưa điền tên sản phẩm")
            End If
        Else ' Them
            If txtTen.Text <> "" Then
                spDT = New SanPhamDT(txtTen.Text, trigia, donvitinh)
                spBL = New SanPhamBL
                spBL.InUp(spDT, True)
                MessageBox.Show("Thêm mới thành công!")
            Else
                MessageBox.Show("Chưa điền tên sản phẩm")
            End If
        End If
        dgv.Enabled = True
        btnThem.Enabled = True
        btnCapNhat.Enabled = True
        loadDGV(" WHERE MaLoai <> -1")
        txtTen.Text = ""
        txtDVT.Text = ""
        txtTGKhac.Text = ""
        CBox()
        gbTTSP.Enabled = False
    End Sub

    Private Sub xuli_updatestate()
        If dgv.SelectedRows.Count > 0 Then
            Dim state As Integer
            Dim statemess As String
            Dim ma As Integer = Convert.ToInt32(dgv.SelectedRows(0).Cells(0).Value)
            If dgv.SelectedRows(0).DefaultCellStyle.BackColor = Color.Aqua Then
                state = 1
                statemess = "Bán"
            Else
                state = 2
                statemess = "Ngưng bán"
            End If
            If MessageBox.Show("Bạn có chắc muốn chuyển sản phẩm sang trạng thái <" + statemess + "> không ?", "Thông báo Trạng thái", MessageBoxButtons.YesNo) = MsgBoxResult.Yes Then
                spDT = New SanPhamDT(ma, state)
                spBL = New SanPhamBL
                spBL.UpState(spDT)
                MessageBox.Show("Sản phẩm đã chuyển sang " + statemess + "", "Thông báo")
            End If
        End If
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        xuli_suathem()
    End Sub

    Private Sub dgv_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgv.DoubleClick
        xuli_updatestate()
        loadDGV(" WHERE MaLoai <> -1")
    End Sub

    Private Sub chboxTG_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chboxTG.CheckedChanged
        If chboxTG.Checked = True Then
            txtTGKhac.Enabled = True
            cbTriGia.Enabled = False
            txtTGKhac.Focus()
        Else
            cbTriGia.Enabled = True
            txtTGKhac.Enabled = False
            cbTriGia.Focus()
        End If
    End Sub

    Private Sub chboxDVT_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chboxDVT.CheckedChanged
        If chboxDVT.Checked = True Then
            txtDVT.Enabled = True
            cbDVT.Enabled = False
            txtDVT.Focus()
        Else
            txtDVT.Enabled = False
            cbDVT.Enabled = True
            cbDVT.Focus()
        End If
    End Sub
End Class